/**
 * Copyright 2018 feel开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.feel.web.controller.oss.cloud;


import cn.hutool.json.JSONUtil;
import com.feel.common.constant.ConfigConstant;
import com.feel.common.utils.StringUtils;
import com.feel.system.service.ISysConfigService;
import org.springframework.stereotype.Service;

/**
 * 文件上传Factory
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 10:18
 */
@Service
public final class OSSFactory {
//    private static ISysConfigService sysConfigService;
//
//    static {
//        OSSFactory.sysConfigService = (ISysConfigService) SpringContextUtils.getBean("ISysConfigService");
//    }

    public static CloudStorageService build(ISysConfigService sysConfigService){
        //获取云存储配置信息
        String s = sysConfigService.selectConfigByKey(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY);
        CloudStorageConfig config;
        if(StringUtils.isNotEmpty(s)) {
            config = JSONUtil.toBean(s,CloudStorageConfig.class);
        } else {
            return null;
        }

        if(config.getType() == ConfigConstant.CloudService.QINIU.getValue()){
            return new QiniuCloudStorageService(config);
        }else if(config.getType() == ConfigConstant.CloudService.ALIYUN.getValue()){
            return new AliyunCloudStorageService(config);
        }else if(config.getType() == ConfigConstant.CloudService.QCLOUD.getValue()){
            return new QcloudCloudStorageService(config);
        }

        return null;
    }

}
